package com.edou.WeChat.controller;

import com.alibaba.fastjson.JSONObject;
import com.edou.WeChat.entity.WxInfo;
import com.edou.WeChat.entity.WxSessionParam;
import com.edou.base.controller.BaseController;
import com.edou.base.util.HttpClientUtilForWx;
import com.edou.base.util.HttpResult;
import com.edou.base.util.WxUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author zhangyaofang
 * @create 2018-11-27 10:38 AM
 * @desc 微信权限
 **/
@RestController
@RequestMapping("/wxAuth")
public class WxAuthController extends BaseController{


	/**
	 * 解密
	 * @param wxInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/decrypt")
	@ResponseBody
	public ModelAndView decrypt(@RequestBody WxInfo wxInfo) throws Exception {
		logger.info("解析入参"+wxInfo.toString());
		JSONObject jsonObject = WxUtils.getUserInfo(wxInfo.getEncryptedData(),wxInfo.getSessionKey(),wxInfo.getIv());
		if(jsonObject == null){
			return ajaxJSON(BaseController.Status.error,"解析入参时发生错误");
		}
		return this.ajaxJSON(Status.success,jsonObject);
	}

	@RequestMapping("/sessionKey")
	public ModelAndView getSessionId(@RequestBody @Valid WxSessionParam wxSessionParam, BindingResult r){
		/**
		 * 验证参数
		 */
		if(StringUtils.isEmpty(wxSessionParam.getAppId())){
			return ajaxJSON(Status.error,"AppId不可为空");
		}
		if(StringUtils.isEmpty(wxSessionParam.getJsCode())){
			return ajaxJSON(Status.error,"JsCode不可为空");
		}

		String appId = wxSessionParam.getAppId();
		String appSecret = "c03327bbb74304abb65e54275718c20a";
		String jsCode = wxSessionParam.getJsCode();
		String grantType = wxSessionParam.getGrantType();

		if(StringUtils.isEmpty(grantType)){
			grantType = "authorization_code" ;
		}

		StringBuilder stringBuilder = new StringBuilder("https://api.weixin.qq.com/sns/jscode2session?appid=");
		stringBuilder.append(appId);
		stringBuilder.append("&secret=");
		stringBuilder.append(appSecret);
		stringBuilder.append("&js_code=");
		stringBuilder.append(jsCode);
		stringBuilder.append("&grant_type=");
		stringBuilder.append(grantType);


		HttpResult result = HttpClientUtilForWx.get(stringBuilder.toString());

		JSONObject json = JSONObject.parseObject(result.getContent());
		if(!StringUtils.isEmpty(json.get("errcode"))){
			logger.error("获取sessionKey失败，返回值={}"+json.toJSONString());
			return ajaxJSON(Status.error,"授权失败");
		}

		return this.ajaxJSON(Status.success, json);
	}


	/**
	 * 返回json数据
	 * @param status
	 * @param root
	 * @return
	 */
	protected ModelAndView ajaxJSON(Status status, JSONObject root){
		ModelAndView view = new ModelAndView("jsonView");
		Map<String,Object> data = view.getModel();
		data.put(STATUS_PARAMETER_NAME, status);
		data.put(JSONARRAY_PARAMETER_NAME,root);
		return view;
	}
}
